package com.example.azheng.rxjavamvpdemo.mvp.presenter;

import com.example.azheng.rxjavamvpdemo.base.BasePresenter;
import com.example.azheng.rxjavamvpdemo.bean.BaseArrayBean;
import com.example.azheng.rxjavamvpdemo.bean.BaseObjectBean;
import com.example.azheng.rxjavamvpdemo.bean.code.CodeBean;
import com.example.azheng.rxjavamvpdemo.mvp.contract.RegisterContract;
import com.example.azheng.rxjavamvpdemo.mvp.model.RegisterModel;
import com.example.azheng.rxjavamvpdemo.net.RxScheduler;

import java.util.Map;

import io.reactivex.functions.Consumer;

public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {
    private RegisterContract.Model model;

    public RegisterPresenter() {
        model = new RegisterModel();
    }

    @Override
    public void register(Map map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.register(map)
                .compose(RxScheduler.<BaseObjectBean<Object>>Flo_io_main())
                .as(mView.<BaseObjectBean<Object>>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean<Object>>() {
                    @Override
                    public void accept(BaseObjectBean<Object> bean) throws Exception {
                        mView.hideLoading();
                        mView.registerSuccess(bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });

    }

    @Override
    public void getCode() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        model.getCode()
                .compose(RxScheduler.<BaseObjectBean<CodeBean>>Flo_io_main())
                .as(mView.<BaseObjectBean<CodeBean>>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean<CodeBean>>() {
                    @Override
                    public void accept(BaseObjectBean<CodeBean> bean) throws Exception {
                        mView.getCodeSuccess(bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                    }
                });
    }
}
